<?php

/**
 * 后台友情链接模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: link.class.php 1402 2011-03-03 10:15:34Z zhangwentao $
 * http://www.t-mac.org；
 */
class link extends Model
{

    private $link_table;
    private $category_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->link_table = '#@__links';
        $this->category_table = '#@__categories';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addlink($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->link_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个友情链接信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getlinkInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->link_table WHERE link_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有链接
     * return article_class,pages
     */
    public function getlinkList($url = null, $cat_id, $link_recommend, $search_keyword)
    {

        if ($url == '')
            $url = 'index.php?m=admin/link';
        $sql_count = "SELECT COUNT(*) FROM $this->link_table WHERE 1 = 1 ";
        $sql = "SELECT a.link_id, a.link_name, a.link_recommend, a.link_url, a.link_type_radio, a.link_state_radio, a.link_order, a.link_time, b.cat_name, c.cat_name AS pname FROM $this->link_table a LEFT JOIN $this->category_table b ON a.cat_id = b.cat_id LEFT JOIN $this->category_table c ON b.cat_pid = c.cat_id WHERE 1=1 ";
        if ($cat_id != '') {
            $cat_ids = $this->getSubclassId($cat_id);
            $cat_ids[] = $cat_id;
            $cat_ids = implode(',', $cat_ids);
            $sql_count .= "AND cat_id IN($cat_ids)";
            $sql .="AND a.cat_id IN($cat_ids)";
            $url .= "&cat_id=$cat_id";
        }
        if (!empty($link_recommend) && $link_recommend != 'none') {
            $sql_count .= " AND link_recommend = $link_recommend ";
            $sql .=" AND a.link_recommend = $link_recommend";
            $url .= "&link_recommend=$link_recommend";
        }
        if ($search_keyword != '') {
            $sql_count .= " AND link_name LIKE '%" . $search_keyword . "%'";
            $sql .=" AND a.link_name LIKE '%" . $search_keyword . "%'";
            $url .= "&search_keyword=$search_keyword";
        }
        $url .= '&page=';

        $count = $this->db->getOne($sql_count);
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(20);
        $limit = $pages->getSqlLimit();
        $sql .= " ORDER BY a.link_order DESC,a.link_id DESC LIMIT $limit";
        $rs = $this->db->getAll($sql);
        //友情状态数组
        $link_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        //友情类型数组
        $link_type_radio_ary = UtilityConfig::link_type_radio_ary();
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['state_name'] = $link_state_radio_ary[$v['link_state_radio']];
                $rs[$k]['type_name'] = $link_type_radio_ary[$v['link_type_radio']];
                $rs[$k]['time'] = date('Y-m-d H:i:s', $v['link_time']);
                $rs[$k]['recommend'] = $v['link_recommend'] == 0 ? '不是推荐' : '推荐';
            }
        }

        //把文章的当前page写到cookies里
        HttpResponse::setCookie('link_page', $pages->getNowPage());

        if ($count == 0) {
            $ErrorMsg = "暂无链接!   <a href='index.php?m=admin/link.add' class='link_a'>点我来添加新链接</a>";
        } else {
            $ErrorMsg = '';
        }

        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * del
     * @param int $id
     */
    public function dellink($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->link_table WHERE link_id in ($id)");
        return $rs;
    }

    /**
     * 根据$cat_id递归取其子类的id
     * @global <type> $ids
     * @param <type> $cat_id
     * @return <type>
     */
    public function getSubclassId($cat_id)
    {
        global $ids; //定义全局变量 返回值
        $sql = "SELECT cat_id FROM $this->category_table WHERE cat_pid = $cat_id ORDER BY cat_order DESC, cat_id ASC";
        $rs = $this->db->cacheGetAll($sql);
        if (is_array($rs) && count($rs) > 0) {
            foreach ($rs as $k => $v) {
                $ids[] = $v['cat_id'];
                $this->getSubclassId($v['cat_id']); //存在下一级就再次递归调用函数
            }
        }
//        $ids[] = $cat_id;
        return $ids;
    }

}